#!/usr/bin/env python
""" AnyBackup is a windows specific backup program to keep multiple volumes in sync with multiple volumes
    Copyright (C) 2011  Andrew Hopkinson

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
    Exported Classes:
    
    dirTree -- a wrapper for TreeCtrl which makes populating a tree with a directory structure trivial
    """
    
import wx
import re

class dirTree(wx.TreeCtrl):
    """ a wrapper for TreeCtrl which makes populating a tree with a directory structure trivial """
    def __init__(self,parent,id,fileUtil):
        wx.TreeCtrl.__init__(self,parent,id,wx.DefaultPosition,wx.Size(250,-1),wx.TR_SINGLE|wx.TR_HAS_BUTTONS|wx.SUNKEN_BORDER)
        self.fileUtil = fileUtil
    
    def populate(self,driveRef):
        self.DeleteAllItems()
        root = wx.TreeItemData()
        root.SetData(driveRef.getRootFile())
        icon = self.fileUtil.getIcon(driveRef.getRootFile())
        self.AddRoot(driveRef.getName(),icon,icon,root)
        item = self.GetRootItem()
        dirs = []
        if not driveRef.getRootFile():
            return
        for file in driveRef.getRootFile().getFiles():
            if file.isDir():
                dirs.append(file)
        if dirs:
            self.SetItemHasChildren(item,True)
            self.populateTree(item,driveRef.getRootFile().getFiles())
            self.Expand(item)
    
    def populateTree(self,parent,files):
        #SORTME
        for file in files:
            if file.isDir():
                data = wx.TreeItemData()
                data.SetData(file)
                icon = self.fileUtil.getIcon(file)
                result = self.AppendItem(parent,unicode(file.getName()).encode('utf-8'),icon,icon,data)
                dirs = []
                for subFile in file.getFiles():
                    if subFile.isDir():
                        dirs.append(subFile)
                if dirs:
                    self.SetItemHasChildren(result,True)
    
    